Database-Independent Mechanism for Retrieving Relational Data as XML

ABSTRACT

Techniques are disclosed for retrieving both data stored in relational form and data stored in XML form responsive to a single XQuery query. The techniques allow applications to retrieve relational data residing in various relational database management systems by using XQuery and XQuery for Java. In one such embodiment, an embeddable serverless computer component implements the techniques, and includes a combination of one more relational adaptors and a mediator to access relational data and transform it into appropriate XML. The techniques are embeddable, and can be standards-based, database-independent, and operating-system independent. Optimized performance and scalability from desktop to enterprise applications are enabled as the number of users and/or database size increases.

RELATED APPLICATIONS

This application is a continuation of prior U.S. application Ser. No.11/283,224, filed Nov. 18, 2005, which claimed the benefit of U.S.Provisional Application No. 60/630,837, filed on Nov. 23, 2004. Each ofthe applications is herein incorporated in its entirety by reference.

FIELD OF THE INVENTION

The invention relates to data processing, and more particularly, to datatransformation between relational data and XML data.

BACKGROUND OF THE INVENTION

Primarily queried from relational databases, business-critical data isincreasingly required to be exchanged and processed as either XML data,or a combination of XML data and relational data. However, using XMLwith relational databases has its challenges. For instance, each majordatabase vendor provides XML extensions, but these extensions aredifferent for each vendor and do not allow applications to be portableamong databases. Some developers use XML standards (such as DOM, SAX, orStAX) in combination with database standards (such as ODBC or JDBC), butthis approach requires developers to write and maintain large amounts ofcode.

Recognizing such challenges, the World Wide Web Consortium (W3C)developed XQuery (short for XML Query) as a uniform language for dataexchange. In the same way that SQL provides a query language forrelational databases, XQuery provides a common language for accessingXML data stores and any data that can be viewed as XML (such asrelational data). Thus, the amount of code developers must write andmaintain to use XML and relational data together is reduced. The W3CXQuery specification and its supporting documentation are hereinincorporated by reference in their entirety. As are the XML PathLanguage (XPath) specification and its supporting documentation. XQueryuses XPath syntax to address specific parts of an XML document.

However, there is currently no mechanism that allows applicationdevelopers to retrieve relational data as XML using both a standardquery language and a standard API. Rather, application developers mustrely on either home-grown solutions or on a vendor-specific combinationof proprietary API (or API extensions) and a standard query language,like SQL/XML or XQuery.

What is needed, therefore, are techniques that allows applicationdevelopers to retrieve relational data as XML using both a standardquery language and a standard API.

SUMMARY OF THE INVENTION

One embodiment of the present invention provides a method fortransforming between relational data and XML data, using an embeddedserverless computer component. The method includes receiving an XQueryquery from a Java application via an XQuery API for Java (XQJ), theapplication attempting to access a relational data source. The methodcontinues with generating XML results responsive to the XQuery query,based on data from the relational data source. The method also includessending the XML results to the Java application via the XQJ. In one suchcase, generating XML results responsive to the XQuery query includesretrieving both data stored in relational form and data stored in XMLform responsive to a single XQuery query. The method may includeanalyzing the XQuery query, and dividing it into one or more XQueryexpressions to be processed. In one such case, for at least one of theXQuery expressions, the method continues with determining if an XML orrelational data source is being queried. Generating XML resultsresponsive to the XQuery query may further include combining resultsfrom multiple data sources. The method may include executing at leastone XQuery expression in the embedded serverless computer component,using data retrieved from the relational data source. Generating XMLresults responsive to the XQuery query may be preceded by determining ifan XML data source is also being queried. In one such case, and inresponse to relational data source queries, generating XML resultsincludes: translating the XQuery query into an SQL query, querying therelational data source with the SQL query, receiving the SQL queryresults and mapping the SQL query results into XML. In another suchcase, and in response to XML data source queries, generating XML resultsincludes: querying the XML data source with the XQuery query, andreceiving the XML XQuery query results. The method may include usingvalue comparisons instead of general comparisons, to make queries runfaster. The method may include using query pooling so as to reusequeries that have previously been executed, to make queries run faster.The method may include using at least one of option declarations toprocess queries, and extension expressions to process expressions in aquery.

Another embodiment of the present invention provides a machine-readablemedium (e.g., compact disk, diskette, server, memory stick, or harddrive) encoded with instructions, that when executed by a processor,cause the processor to carry out a process for transforming betweenrelational data and XML data, using an embedded serverless computercomponent. This process can be, for example, similar to or a variationof the previously described method.

Another embodiment of the present invention provides an embeddableserverless computer component system for transforming between relationaldata and XML data. The system can install, for example, like a driver.The system includes an XQuery interface for receiving an XQuery queryfrom an application attempting to access a relational data source. Thesystem further includes an XQuery engine for generating XML resultsresponsive to the XQuery query, based on data from the relational datasource, and sending the XML results to the application via the XQueryinterface. The XQuery interface can be, for example, an XQuery API forJava (XQJ) module and the application can be a Java application. In oneparticular case, the system is configured for retrieving both datastored in relational form and data stored in XML form responsive to asingle XQuery query. In another particular configuration, the XQueryengine includes a mediator module for analyzing the XQuery query, anddividing it into one or more XQuery expressions to be processed. In onesuch case, and for at least one of the XQuery expressions, the mediatormodule is for determining if an XML or relational data source is beingqueried. The mediator module can be further configured for combiningresults from multiple data sources. The mediator module can beconfigured for executing at least one XQuery expression using dataretrieved from the relational data source. The XQuery engine may includean SQL adapter for translating a relational data source XQuery queryinto an SQL query, querying the relational data source with the SQLquery, receiving the SQL query results, and mapping the SQL queryresults into XML. The XQuery engine may include an XML adapter forquerying the XML data source with an XML data source XQuery query, andreceiving the XML XQuery query results. The system may be configured forusing value comparisons instead of general comparisons, to make queriesrun faster, and/or for using query pooling so as to reuse queries thathave previously been executed, to make queries run faster. The systemmay be configured for using at least one of option declarations toprocess queries, and extension expressions to process expressions in aquery.

The system functionality can be implemented, for example, in software(e.g., executable instructions encoded on one or more computer-readablemediums), hardware (e.g., gate level logic), firmware (e.g., one or moremicrocontrollers with embedded routines), or some combination thereof,or other suitable means.

The features and advantages described herein are not all-inclusive and,in particular, many additional features and advantages will be apparentto one of ordinary skill in the art in view of the figures anddescription. Moreover, it should be noted that the language used in thespecification has been principally selected for readability andinstructional purposes, and not to limit the scope of the inventivesubject matter.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a block diagram of an XQuery system configured in accordancewith one embodiment of the present invention.

FIG. 2 is a block diagram of an XQuery module of the system of FIG. 1,configured in accordance with one embodiment of the present invention.

FIG. 3 illustrates an method for transforming between relational dataand XML data, in accordance with one embodiment of the presentinvention.

DETAILED DESCRIPTION OF THE INVENTION

Techniques are disclosed that allow application developers to retrieverelational data as XML using both a standard query language and astandard API. The techniques are embeddable, and can be standards-based,database-independent, and operating-system independent. No server isrequired. Optimized performance and scalability from desktop toenterprise applications are enabled as the number of users and/ordatabase size increases.

Overview

One embodiment of the present invention is a serverless computercomponent (e.g., shared library or executable) for retrieving relationaland/or XML data responsive to a single XQuery query from an application.In one particular configuration, this component is implemented as a Javaimplementation of XQuery that uses the XQuery API for Java (XQJ).Applications are allowed to query XML sources, relational sources, or acombination of the two. The results are integrated for XML-based dataexchange, XML-driven Web sites, and other applications that require orleverage the power of XML. The XML results can be returned in any numberof forms, such as text, DOM (document object model), SAX (simple API forXML), or StAX (streaming API for XML).

Such an embodiment can run in any Java environment, on any operatingsystem, using any number of databases, with or without applicationservers or other servers. Developers and independent software vendorswho need to manage heterogeneous data sources in XML applications arethus allowed to work with standards instead of proprietary querylanguage extensions and APIs. Numerous relational and XML sources can beused. In addition, XML queries that access data from relational sourcesare optimized to achieve high performance.

As previously explained, XQuery is a query language for XML. XQuery isused to query XML in a manner that is similar to the way SQL is used toquery relational tables. Typically, SQL queries create tables torepresent the result of a query, and XQuery queries create XML torepresent the result of a query. This resulting XML can be as complex asnecessary. For example, the result of an XQuery query may be a complexdocument, such as an inventory report, a document with dynamic content,or a SOAP (Simple Object Access Protocol) message. The result of anXQuery query can also be as simple as a single integer. For example, thequery might count the number of items that satisfy a condition. InXQuery terminology, the results of an XQuery query is an instance of theXQuery data model. The term “XML result” is used herein for simplicity,to refer to the results of an XQuery query.

XQuery goes beyond the functionality of relational query languages, andincludes many features traditionally found in functional programminglanguages. Just as SQL is a relational query language and Java is anobject-oriented language, XQuery is often thought of as a native XMLprogramming language. In XQuery, the only complex data structure is XML,and the operations that are regularly needed for processing XML aredirectly supported in a convenient manner. XQuery can easily search anyXML structure with path expressions, create any XML structure usingconstructors, and transform XML structures using FLWOR(For-Let-Where-Order-Return) expressions. In addition, XQuery simplifiesthe tasks encountered when working with namespaces or data types.

Because XML is used to represent and transfer data from a wide varietyof sources, XQuery is also widely used for data integration. Even whendata is not physically stored as XML, XQuery can be used with databasesmiddleware that provides an XML view of the data. For instance, SOAP maybe used to acquire data from a variety of sources, and XQuery may beused to query the resulting SOAP messages (in XML) together with datafound in a relational database (using an XML view). The SOAPspecification is currently maintained by the XML Protocol Working Groupof the W3C, and is herein incorporated in its entirety by reference.

The XQuery API for Java (XQJ) is designed to support the XQuerylanguage, just as the JDBC API supports the SQL query language. Inparticular, XQJ enables a Java application to submit XQuery queries toan XML data source and process the results. In this sense, XQJ is toXQuery what JDBC is to SQL XQJ is based on XQuery sequences rather thanrelational result sets. The XQJ standard (JSR 225), which is hereinincorporated in its entirety by reference, is currently maintained bythe Java Community Process.

The following example illustrates the basic steps that an applicationperforms to execute an XQuery query using an embodiment of the presentinvention. To simplify the code, the example contains no error handling.However, error handling may also be integrated into the code, as will beapparent in light of this disclosure. In this example, the applicationestablishes a connection to a relational database using a DDXQDataSourceinstance. Note that DDXQDataSource refers to an XQuery (XQ) data sourcefunction, and the prefix DD indicates a DataDirect implementation of anXQuery data source function. DataDirect Technologies is an operatingcompany of Progress Software Corporation. Other XQuery data sourcefunctions can be used as well as will be apparent in light of thisdisclosure, and the present invention is not intended to be limited toany one particular implementation. This statement equally applies toother “DD” or “dd” implementations referred to herein.

//import the XQJ classes import com.ddtek.xquery.*; importcom.ddtek.xquery.xqj.mediator.DDXQDataSource; // establish a connectionto a data source DDXQDataSource ds = new DDXQDataSource( );ds.setJdbcUrl(“jdbc:xquery:sqlserver://server1:1433;databaseName=stocks”); XQConnection conn = ds.getConnection(“myuserid”,“mypswd”); //create an expression object that is used to execute an XQuery queryXQExpression expr = conn.createExpression( ); // the XQuery query Stringes = “for $h in collection(‘HOLDINGS’)/HOLDINGS ” +    “where$h/STOCKTICKER=‘AMZN’ ” +    “return $h”; // execute the XQuery queryXQResultSequence result = expr.executeQuery(es);System.out.println(result.getSequenceAsString( )); // free all resourcesresult.close( ); expr.close( ); conn.close( );

System Architecture

FIG. 1 is a block diagram of an XQuery system configured in accordancewith one embodiment of the present invention. As can be seen, the systemincludes an XQuery module 120 that processes XQuery queries 110 fromJava application 105. In response to an XQuery query 110, the XQuerymodule 120 accesses the appropriate relational sources 125 and/or XMLsources 130. XML results 115 are then provided to the application 105.

The system can be implemented in any computing environment. Forinstance, the Java application 105 and XQuery module 120 can beexecuting on a conventional computing system, such as a desktop, laptop,or personal digital assistant (PDA). Alternatively, the Java application105 and XQuery module 120 can be executing on different computingsystems that are communicatively coupled (e.g., via a network or directconnection). Relational sources 125 and XML sources 130 can be includedin the same computing system as the XQuery module 120, or are otherwiseaccessible to the XQuery module 120 (e.g., via a network). Numerouscomputing environments can be used, as will be apparent in light of thisdisclosure.

The XQuery module 120 of this embodiment operates as a serverlesscomputer component for retrieving relational and/or XML data responsiveto a single query 110 from the Java application 105. The module 120 canbe embedded, and installs like a driver (e.g., JDBC driver). In oneparticular configuration, this XQuery module 120 is a Javaimplementation of XQuery that uses the XQuery API for Java (XQJ) to sendqueries and return the XML results 115 to the application 105. Thus,application 105 is allowed to query XML sources 130, relational sources125, or a combination of the two. The XQuery module 120 retrieves dataresponsive to the query from the relational sources 125 and XML sources130. Data from relational sources 125 is mapped into XML format. TheXQuery module 120 then integrates data from each of the sources into asingle response, and sends the XML results 115 to the application 105via XQJ (or other suitable API).

Note that application 105 can be any application that requires orotherwise uses XML, and need not be limited to Java applications. Forinstance, the application can be a .Net application. Likewise, theapplication can be a C, C++, or other object-oriented application. Othersuch XML-centric applications will be apparent in light of thisdisclosure.

As previously explained, the XML results can be returned in any numberof forms, such as text, DOM, SAX, StAX, or other suitable forms. Such anembodiment can run in any Java environment (or other suitableenvironment), on any operating system (e.g., Windows, Unix, Linux, MAC,etc), using any number of relational databases (e.g., Oracle, DB2, SQLServer, Sybase, MySQL, etc), with or without application servers orother servers. Numerous relational and XML sources are supported, suchas databases through a JDBC (Java Database Connectivity) connection, XMLfiles through uniform resource identifier (URI) schemes (e.g., http,ftp, and file), and XML represented through an XML API (e.g., DOM).

Embeddable XQuery Module

FIG. 2 is a block diagram of an XQuery module 120 of the system of FIG.1, configured in accordance with one embodiment of the presentinvention. As can be seen, the module 120 includes an XQuery interfaceand an XQuery engine. In this particular configuration, the XQueryinterface is implemented with an XQJ module 205. The XQuery engine,which generates XML results responsive to the XQuery query, isimplemented with a mediator module 210, a number of SQL adapters 215,and a number of XML adapters 220.

In operation, a Java application (or other suitable application) passesan XQuery query to the XQJ module 205. The XQJ module 205 implements theXQuery API for Java (XQJ). As previously explained, XQJ enables a Javaapplication to submit XQuery queries to an XML data source and processthe results. The XQuery API for Java of the XQJ module 205 can beimplemented in accordance with the previously incorporated XQJ standard(JSR 225), or other such suitable standards (such as those based on ordeveloped from, or otherwise modeled after, JSR 225). Alternatively, themodule 205 can be implemented with custom or other non-standards basedtechnology that is programmed or otherwise configured to enable anXML-centric application to submit XQuery queries to XML data sources.

The mediator module 210 analyzes the XQuery query and divides it intoone or multiple XQuery expressions to be processed by the adaptors 215and/or 220. The mediator module 210 sends the XQuery query to the SQLadaptor or the XML adaptor based on its analysis.

If a relational source 125 is queried, the mediator module 210 sends theXQuery query to the corresponding SQL adaptor 215. The SQL adaptor 215translates the XQuery query into SQL, which is then used to query theappropriate relational database 125. The SQL adaptor 215 receives theSQL results and maps them into XML. Example SQL/XML mappings for variousrelational databases (e.g., DB2, Oracle, and SQL Server) are shown inthe previously incorporated U.S. Provisional Application No. 60/630,837(e.g., Tables A-49 to A-50). Any number of SQL/XML mapping schemes canbe used (e.g., see mapping rules of SQL 2003 standard, which is hereinincorporated in its entirety by reference).

If an XML source 130 is queried, the mediator module 210 sends theXQuery query to the corresponding XML adaptor 220, which executes theXQuery query and returns XML results. Each of the adapters 215 and 220send their XML results to the mediator module 210. If the XML resultsare obtained from more than one source, the mediator module 210 combinesthe results. The Java application receives the results as XML, via theXQJ module 205.

Each of the SQL adapters 215 and XML adapters 220 may be configured withquery optimizer capability, for determining efficient query plans (e.g.,with respect to CPU and/or I/O usage cost). For instance, the SQLadapters 215 can be configured to use join optimization, use temporarytables to store intermediate results, and access only the relevantsubset of a table when a query joins two tables. In any case, XMLqueries that access data from relational sources can be optimized toachieve high performance. Numerous optimization schemes can be usedhere, as will be apparent in light of this disclosure.

The XQuery module 120 (including each of its components) can beimplemented, for example, with software (e.g., C, C++, Java, or otherobject-oriented programming language), hardware (e.g., gate-levellogic), firmware (e.g., embedded routines executing on one or moremicrocontrollers of a host device), or some combination thereof. Notethat other embodiments may be configured differently than the one shown.For instance, another embodiment may implement the XQuery module 120 ona server (or otherwise in conjunction with a server), if so desired. Inone such embodiment, the Java application may be remotely accessing theserver, for example, via the Internet or other network. Also, databasescontaining the relational and XML sources may be local or remote to themodule 120. Many variations on the system layout will be apparent inlight of this disclosure.

Connecting to XML data sources and relational data sources can beachieved, for example, using XQJ to construct a DDXQDataSource instancein the Java application explicitly, or by using XQJ to load aDDXQDataSource object from JNDI (Java Naming Directory Interface, whichis a standard API/SPI for J2EE naming interface), or by configuring anXQuery source configuration file and constructing a DDXQDataSourceinstance in your Java application to reference the source configurationfile (the format of a source configuration file is XML).

Specifying connection information explicitly in the Java applicationusing a DDXQDataSource instance allows the information to be codeddirectly in the Java application. On the other hand, using aDDXQDataSource object loaded from JNDI can be a convenient way to manageconnections because the connection information is created and managedoutside the applications that use it. Because connection information isoutside the application, the effort required to reconfigure theenvironment when an infrastructure change occurs is minimal. Forexample, if a database is moved to another server and uses a differentport number, only the relevant properties of the data source object needto be changed. An application using the database does not need to changebecause the application only references the logical name of the datasource object in JNDI. A source configuration file also allowsconnection information for XML and relational data sources to bespecified. Similar to using a DDXQDataSource object loaded from JNDI, asource configuration file allows connection information to be managedoutside the application, which minimizes changes to the Javaapplication. In addition, most XQuery settings can be set using XQJ.However, a source configuration file can be used if it is desired tochange default values of certain XQuery settings, such as the followingSQL/XML variables: XMLFOREST, TARGETNS, and Identifier Escaping.

How a typical source configuration file looks depends on whether theXQuery queries access XML and/or relational data sources, as will beapparent in light of this disclosure. For instance, if the Javaapplication executes XQuery queries that access only XML data sources, aexample source configuration file would look like the following:

<?xml version=“1.0” ?> <XQJConnectionxmlns=“http://www.datadirect.com/xquery”> <baseUri>file:///c:/programs/ddxq/examples/</baseUri> </XQJConnection>The baseUri element in this example specifies the base URI used toresolve relative URIs in fn:doc( ). Other elements can be specified in asource configuration file as well, as will be apparent in light of thisdisclosure. A source configuration file specifying connectioninformation to a single relational database, Microsoft SQL Server, inthis example, looks like the following:

<XQJConnection xmlns=“http://www.datadirect.com/xquery”> <JDBCConnection name=“example_connection_name”>  <url>jdbc:xquery:sqlserver://localhost:1433</url>  </JDBCConnection></XQJConnection>

Standard XQuery uses fn:collection( ) to access relational data, butXQuery does not have the concept of a relational database or aconnection. In accordance with an embodiment of the present invention,the XQuery module 120 uses XQJ to specify the required databaseconnections and associate the names specified by fn:collection( ) withthe database tables. Using XQJ, a connection can be created from anXQDataSource instance. The class name of the XQuery XQDataSourceimplementation is: com.ddtek.xquery.xqj.mediator.DDXQDataSource. Thefollowing class provides additional properties for configuringconnections to multiple databases:com.ddtek.xquery.xqj.mediator.DDXQJDBCConnection. Connection informationcan be specified to relational data sources using any of the examplemethods shown in the following examples.

Example 1 Using a DDXQDataSource Instance to Specify ConnectionInformation Explicitly

DDXQDataSource ds = new DDXQDataSource( );ds.setJdbcUrl(“jdbc:xquery:sqlserver://server1:1433;databaseName=stocks”);

Example 2 Using JNDI

Context ctx = new InitialContext( ); DDXQDataSource ds =(DDXQDataSource)ctx.lookup(“holdings_ds”); XQConnection conn =ds.getConnection(“myuserid”, “mypswd”);

Example 3 Using an XQuery Source Configuration File

FileInputStream is = null; DDXQDataSource ds = null; try {  is = newFileInputStream(“C:\config_files\ms_sql_config.xml”);  ds = newDDXQDataSource(is);  if (is != null) is.close( );  }catch(ExceptionanException) {  // do something to handle or report the exception  }

After specifying connection information, the getConnection( ) method canbe invoked to return a connection to the database and, optionally,specify the user name and password for the connection: XQConnectionconn=ds.getConnection(“myuserid”, “mypswd”).

Next, an XQExpression object is created, which executes an XQueryexpression and returns a sequence of results. An XQConnection can createan XQExpression:

XQExpression xqExpression = conn.createExpression( ); FileReaderfileReader = new FileReader(“xquery_file.xq”); XQSequence xqSequence =xqExpression.executeQuery(fileReader);Note that for purposes of this example, the referenced XQuery query isin a separate file named xquery_file.xq. Alternatively, the query can becoded in a String object. Now that the query results are in a sequence,this sequence can be serialized using the getSeuenceAsString( ) method:System.out.println(xqSequence.getSequenceAsString( )); The result lookslike this:

<user>  <name>   <FIRSTNAME>Jonathan</FIRSTNAME>  <LASTNAME>Robie</LASTNAME>  </name>  <stock>  <STOCKTICKER>AMZN</STOCKTICKER>   <SHARES>3000</SHARES>  </stock> <stock>   <STOCKTICKER>EBAY</STOCKTICKER>   <SHARES>4000</SHARES> </stock>  <stock>   <STOCKTICKER>IBM</STOCKTICKER>  <SHARES>2500</SHARES>  </stock>  ... </user>

In the previous example, data in a relational database was queried. Inthe next example, an XML file is queried. Assume a user wants to queryholdings for a specific customer identified by the USERID element in afile named holdings.xml, which looks like this:

<HOLDINGS>  <row>  <USERID>Jonathan</USERID> <STOCKTICKER>AMZN</STOCKTICKER>  <SHARES>3000</SHARES>  </row>  <row> <USERID>Minollo</USERID>  <STOCKTICKER>EBAY</STOCKTICKER> <SHARES>4000</SHARES>  </row> </HOLDINGS>Here is an example XQuery expression that returns holdings for acustomer named Jonathan:

doc(“holdings.xml”)//HOLDINGS [USERID=“Jonathan”]

Now assume the user wants to return holdings for other customers. If anXQuery is written with an external variable that provides the name ofthe customer whose holdings you require, the Java application canspecify the name of the customer before it executes the query. Ifanother external variable is used to represent the document, the Javaapplication can pass any document to the query at runtime. For example:

declare variable $u as xs:string external; declare variable $d asdocument-node(element(*, xdt:untyped)) external; $d//holding[USERID=$u]Example Java code to create a DOM tree and bind it to the variable $d isas follows:

DocumentBuilderFactory factory = DocumentBuilderFactory.- newInstance(); factory.setNamespaceAware(true); DocumentBuilder parser =factory.newDocumentBuilder( ); File xmlFile = new File(“holdings.xml”);Document document = parser.parse(xmlFile);

Once a DOM tree is created, XQJ can be used to bind the DOM tree to avariable and query it. For instance, first, create an expression object,and then bind the document to the variable $d for this expression.

XQConnection conn = ds.getConnection( ); XQExpression xqExpression =conn.createExpression( ); xqExpression.bindNode(new QName(“d”),document);Now, execute the expression and output the result:

FileReader fileReader = new FileReader(“xquery_file.xq”); XQSequencexqSequence = xqExpression.executeQuery(fileReader);System.out.println(xqSequence.getSequenceAsString( ));

Joining Data from XML and Relational Sources:

The previous examples demonstrated how XQJ allows XQuery to operate onrelational and XML file data stores. That functionality can be leveragedto query both types of data stores at the same time using a singleXQuery query. In this next example, assume a Web Service request is usedto provide parameters for the XQuery query, and then query a database tocreate the Web Service response. The Web Service request looks likethis:

<request>  <performance>   <UserId>Jonathan</UserId>  <start>2003-01-01</start>   <end>2003-01-01</end>  </performance></request>The preceding request contains only the SOAP message payload (tosimplify the example, the envelope has been omitted). This request asksfor performance data on a user's portfolio within a specific date range.Now, an XQuery query can be composed that uses the parameters from therequest to create a performance report, which will report theperformance of each stock held by each user during the given range.

let $request := doc(“request.xml”)/request for $user in$request/performance return  <portfolio UserID=“{$user/UserID}”>   {$request }   {   for $h in collection(“HOLDINGS”)   where $h/USERID =$user   return    <stock>    {    $h/STOCKTICKER,    $h/SHARES    }   </stock>   }  </portfolio>

To be consistent with previous examples, establish a connection to thedata source:

XQConnection conn=ds.getConnection( );

Again, create an XQExpression object that executes the XQuery expressionand returns a sequence of results:

FileReader fileReader = new FileReader(“xquery_file.xq”); XQExpressionxqExpression = conn.createExpression( ); XQSequence xqSequence =xqExpression.executeQuery(fileReader);With the query results in a sequence, again serialize this sequenceusing the getSequenceAsString( ) method.

System.out.println(xqSequence.getSequenceAsString( ));

The result looks like this:

<portfolio UserID=“Jonathan”>  <request>   <performance>   <UserId>Jonathan</UserId>    <start>2003-01-01</start>   <end>2004-06-01</end>   </performance>  </request>   <stock>   <STOCKTICKER>PRGS</STOCKTICKER>    <SHARES>23</SHARES>   </stock>  <stock>    <STOCKTICKER>AMZN</STOCKTICKER>    <SHARES>3000</SHARES>  </stock>   <stock>    <STOCKTICKER>EBAY</STOCKTICKER>   <SHARES>4000</SHARES>   </stock>   <stock>   <STOCKTICKER>IBM</STOCKTICKER>    <SHARES>2500</SHARES>   <SHARES>2500</SHARES>   </stock> </portfolio>

Returning Results with Java XML APIs:

Often, applications need to retrieve XQuery results, for example, asDOM, SAX, or StAX. XQSequence, as shown in the previous examples, allowsthe result as a direct mapping of the XQuery sequence. Within anXQSequence, XQItem objects represent each component in an XQuerysequence. Note that instantiating each item in an XQItem object isperformance-expensive, because it requires creating multiple objects.Thus, a cost benefit analysis may be appropriate here. The followingexamples show how to process an XQuery sequence and return the output asDOM, SAX, or StAX.

First, create an XQExpression object that executes the XQuery expressionand returns a sequence of results:

DDXQDataSource ds = new DDXQDataSource( ); ... XQConnection conn =ds.getConnection(“myuserid”, “mypswd”); FileReader fileReader = newFileReader(“xquery_file.xq”); XQExpression xqExpression =conn.createExpression( ); XQSequence xqSequence =xqExpression.executeQuery(fileReader);

To return the output from a sequence as a DOM tree, iterate over eachDOM node in the XQuery sequence to extract the DOM content and print theDOM node to the standard System.out. For example, assuming J2SE 1.4.x,use the following code:

while(xqSequence.next( )){  Node domNode = xqSequence.getNode( ); System.out.println(domNode);  } }Variations on this method (e.g., for J2SE 1.5.X) will be apparent inlight of this disclosure.

To return the output from a sequence as a SAX event stream rather than astring, create a SAX event handler (named SimpleSAXEventHandler in thisexample) that sends the results to the standard System.out as shown inthe following code:

SimpleSAXEventHandler anEventHandler = newSimpleSAXEventHandler(System.out);xqSequence.writeSequenceToSAX(anEventHandler);

To return the output from a sequence as a StAX event stream rather thana string, create a StAX reader as shown in the following code:

XMLStreamReader reader=xqSequence.getSequenceAsStream( );

This StAX reader functionality can be used like any other StAX streamreader. For example, the following code reads one event at a time andprints the event type together with the associated event names.

private static void formatOutput(XMLStreamReader reader) throwsXMLStreamException { while(true){  int event = reader.next( );  if(event== XMLStreamConstants.END_DOCUMENT){  return;  switch (event) {   caseXMLStreamConstants.START_ELEMENT;   System.out.println(“Start tag; ”);  printNames(reader);   break;   case XMLStreamConstants.END_ELEMENT:  System.out.println(“End tag”);   printNames(reader);   break;   caseXMLStreamConstants.START_DOCUMENT;   System.out.println(“Startdocument”);   break;   case XMLStreamConstants.CHARACTERS;  System.out.println(“Text”);   printChars(reader);   break;  } } ...

Preparing XQuery Statements:

Typically, when an XQuery query is executed, the query is parsed andoptimized before it is run. To avoid incurring this overhead each timethe query is used, prepare the query once and execute it multiple times.Here is example code for creating a prepared query:

DDXQDataSource ds = new DDXQDataSource( ); XQConnection conn =ds.getConnection( ); FileReader fileReader = newFileReader(“xquery_file.xq”); XQPreparedExpression preparedExpression =conn.prepareExpression(fileReader); Once the query is prepared, use anexecuteQuery( ) call to execute it, XQSequence xqSequence =preparedExpression.executeQuery( );System.out.println(xqSequence.getSequenceAsString( ));

Queries can accept parameters that can be changed between executions.For example, it may be desirable to prepare a query that selectsholdings based on a particular customer. In the following example query,the value of USERID, a particular customer, changes each time thisXQuery is run.

declare variable $1 as xs:string external;collection(“HOLDINGS”)/HOLDINGS[USERID=$1]The value of $1 can be set using XQJ as shown here. This can be run foreach different user (in this case, twice).

preparedExpression.bindString(new QName(“1”), “Jonathan”); xqSequencexqSequence = preparedExpression.executeQuery( );System.out.println(“\n\nHoldings for Jonathan:\n\n”);System.out.println(xqSequence.getSequenceAsSring( ));preparedExpression.bindString(new QName(“1”), “Minollo”); xqSequencexqSequence = preparedExpression.executeQuery( );System.out.println(“\n\nHoldings for Minollo:\n\n”);System.out.println(xqSequence.getSequenceAsString( ));

Performance Optimization

XQueries carried out in accordance with embodiments of the presentinvention can be optimized or otherwise configured to improve theirperformance, if so desired. Various such performance optimizations,including value comparisons, XQuery compensation, query pooling, optiondeclarations, and extensions expressions, will now be discussed.

Value Comparisons:

When an XQuery encounters comparisons in WHERE clauses or in predicateexpressions and an operand is bound to data in an XML data source,performance can be significantly improved if this operand is known bymediator module 210 of the XQuery module 120 to be a single item.Consider the following query:

for $request in doc(‘file:///c:/in/request.xml’)/request let $ticker :=$request/performance/ticker,  $start := $request/performance/start, $end := $request/performance/end for $h incollection(‘HISTORICAL’)/HISTORICAL where $h/TICKER = $ticker return $hThe XQuery does not know how many ticker, start, or end elements mayoccur in the XML source, so it restricts its rewrites in case there aremore than one of each of these elements. Using value comparisons insteadof general comparisons, as shown in the following code, makes this queryrun faster:

for $request in doc(‘file:///c:/in/request.xml’)/request let $ticker :=$request/performance/ticker,  $start := $request/performance/start, $end := $request/performance/end for $h incollection(‘HISTORICAL’)/HISTORICAL where $h/TICKER eq $ticker return $hNote that this does not work for all data types, because eq is morerestrictive in the types that it accepts, and does less implicitcasting.

XQuery Compensation:

XQuery contains expressions, functions, and operators that cannot bedirectly translated into SQL. For example, fn:tokenize( ) has no SQLequivalent. When an expression cannot be translated to SQL, the XQuerymodule 120 “compensates” the expression; in particular, it executes theexpression in the mediator module 210 using data retrieved from therelational database 125. This provides full-featured XQueryfunctionality. Note that such compensation may be slower than executingan expression in the database. Sometimes, the same result can beobtained by using an expression that does not require compensation.

For example, suppose a user needs to perform string comparisons withdata that contains trailing spaces. The XQuery function normalize-space() can be used, which removes leading and trailing spaces:

for $h in collection(‘stocks.dbo.HISTORICAL’)/HISTORICAL wherenormalize-space($h/TICKER) = ‘AMZN’ return $hHowever, the normalize-space( ) function is compensated, which meansthat the where clause is evaluated in the mediator module 210 ratherthan in the relational database 125, which may slow performance. Forthis example, the most efficient solution is to use the SQL functionrtrim( ), which is available in most commercial databases:

declare function ddtek-sql:rtrim($in as xs:string) as xs:stringexternal; for $h in collection(‘HISTORICAL’)/HISTORICAL whereddtek-sql:rtrim($h/TICKER) = ‘AMZN’ return $h

Query Pooling:

If a Java application executes the same queries again and again,performance can be improved by enabling internal query pooling of theXQuery module 120. Query pooling allows the application to reuse queriesthat have been executed. In one such embodiment of the presentinvention, when query pooling is enabled, the mediator module 210 of theXQuery module 120 caches a specified number of queries executed by anapplication. In one such embodiment, the XQuery module 120 pools queriesexecuted using XQExpression and XQPreparedExpression. Using XQJ, querypooling can be enabled, for example, by specifying the DDXQDataSourceMaxPooledQueries property or using an XQuery source configuration fileby specifying the maxPooledQueries element (a child element of theXQJConnection element). For example, if the DDXQDataSourceMaxPooledQueries property is set to 20, one embodiment of the XQuerymodule 120 caches the last 20 queries executed by the application. Ifthe value set for this property is greater than the number of queriesused by the application, all queries are cached.

Option Declarations:

Option declarations provide parameters that modify how one embodiment ofthe XQuery module 120 processes queries. They are similar to extensionexpressions, which modify how another embodiment of the XQuery module120 processes expressions in a query. In one such embodiment, mediatormodule 210 of the XQuery module 120 is configured to enable optiondeclarations. A global option declaration is used as the default for allXML and relational data sources accessed by XQuery queries in the Javaapplication. One global option declaration supported by an embodiment ofthe XQuery module 120 is referred to as detect-XPST0005. Aconnection-specific option declaration controls the processing of XQueryqueries for a specific connection. Valid option declarations include,for example, detect-XPST0005, sql-decimal-cast, andsql-unicoder-literals. As option declaration can be specified, forexample, using any of the following methods.

Using the Prolog of the XQuery Query:

The syntax for specifying an option declaration in a query is: declareoption ddtek:name_of_option_declaration “value”; For example declareoption ddtek:sql-unicode-literals “yes”.

Using the Properties of the DDXQDataSource and DDXQJDBCConnection Class:

To specify an option declaration as global, use the Pragmas property ofthe DDXQDataSource class. To specify an option declaration asconnection-specific, use the JdbcPragmas property of the DDXQDataSourceclass or the Pragmas property of the DDXQJDBCConnection class. Using anXQuery source configuration file: To specify an option declaration asglobal, use the pragma element (a child element of the XQJConnectionelement). To specify an option declaration as connection-specific, usethe pragma element (a child element of the JDBCConnection element).

In the following example, detect-XPST0005=no is specified as a globaloption declaration. This disables the XPST0005 error, which is raisedduring static analysis under certain conditions. In addition, theexample specifies sql-decimal-cast==20, 15 as a connection-specificoption declaration. This instructs the XQuery module 120 to process aquery with a specific precision and scale for xs:decimal( ) values.

DDXQDataSource ds = new DDXQDataSource( );ds.setJdbcUrl(“jdbc:xquery:sqlserver://server1:1433;databaseName=stocks”); ds.setPragmas(“detect-XPST0005=no”);ds.setJdbcPragmas(“sql-decimal-cast=20,15”) XQConnection conn =ds.getConnection(“myuserid”,“mypswd”); XQExpression xqExpression =connection.createExpression( ); FileReader fileReader = newFileReader(“xquery_file.xq”); XQSequence xqSequence =xqExpression.executeQuery(fileReader);

The option declaration detect-XPST0005 determines whether err:XPST0005is raised during static analysis. If set to yes (the default), the errormessage err:XPST0005 is raised if the static data type assigned to anXQuery expression other than the expression ( ) or data(( )) is void( ).For example, this error is raised if an XQuery can statically determinethat a path expression can never match an item (e.g., because the nameof an element is misspelled in a NameTest). If set to no, theerr:XPST0005 is not raised and the expression is evaluated. This optiondeclaration can be specified as a global or connection-specific optiondeclaration.

The option declaration sql-decimal-cast determines precision and scaleused for xs:decimal( ). If a precision and scale are not specified, thefollowing example default values are used:

DB2: 30, 15

Oracle: Not applicable

Microsoft SQL Server: 38, 19

Sybase: 38, 19

You can override the default by using this option declaration. Thisoption declaration can be specified as a connection-specific optiondeclaration.

The option declaration sql-unicode-literals {yes|no} determines whetherXQuery literals are translated to SQL literals escaped with theAlternate National Character Set escape character N. If set to yes,XQuery literals are translated to SQL literals escaped with theAlternate National Character Set escape character N. Set the value ofthis option declaration to yes when a SQL literal contains charactersthat cannot be translated to the code page of your database. Then,execute the query using this option declaration. If set to no (thedefault), XQuery literals are not translated. Note that some databasesare significantly slower than others when Unicode comparisons areperformed. This option declaration can be specified as aconnection-specific option declaration.

Extension Expressions:

Extension expressions provide parameters that modify how one embodimentof the XQuery module 120 processes expressions in a query. Extensionexpressions can be specified only in the body of a query. In oneembodiment of the XQuery module 120, mediator module 210 is configuredto implement the extension expression of evaluate-in-memory. The syntaxfor specifying an extension expression in a query is:(#ddtek:name_of_extension_expression #). For example:(#ddtek:evaluate-in-memory #).

Suppose a user wants to perform data analysis using the following query,which accesses the HISTORICAL database table and returns the ratio ofthe value of ADJUSTEDCLOSE to the value of ACTUALCLOSE for a particulardate.

for $h in collection(‘HISTORICAL’)/HISTORICAL where $h/TICKER = ‘AMZN’return  <historical>  {$h/DATETRADED}  {$h/ADJUSTEDCLOSE div$h/ACTUALCLOSE}  </historical>Suppose that ACTUALCLOSE is 0 for one or multiple dates because of adata entry error. In XQuery, division by 0 raises an error for decimaland integer data types, but not for float and double data types. Theuser can avoid an error by casting the ratio to a double data type andperforming the division in memory by specifying the evaluate-in-memoryextension expression for the division expression as shown in thefollowing query:

for $h in collection(‘HISTORICAL’)/HISTORICAL where $h/TICKER = ‘AMZN’return  <historical>  {$h/DATETRADED}  {(# ddtek:evaluate-in-memory #) {xs:double($h/ADJUSTEDCLOSE) div $h/ACTUALCLOSE}}  </historical>

The evaluate-in-memory extension expression specifies an expression thatis evaluated in memory as XQuery, ensuring that it will not betranslated to SQL for evaluation in a relational data source. Thisextension expression can be used, for example, to ensure that XQuerymodule 120 uses strictly conforming XQuery behavior when processing datafrom relational data sources. For relational data sources, XQuery module120 sometimes uses compensation to allow expressions to be evaluatedefficiently (as previously described). When strict conformance to theXQuery specification is more important than efficient data handling, usethis extension expression.

The evaluate-in-memory extension expression can also be used, forexample, to provide XQuery functionality not typically provided forrelational data sources. For instance, use this extension expression toperform path expressions on XML stored in the database. This settingensures maximum XQuery conformance, but can significantly degradeperformance depending on how it is used. For example, if used in a whereclause of a FLWOR expression, it can force all rows of a database tableto be evaluated in memory, which degrades performance. The expressionused for evaluation in memory cannot contain the following functions:fn:collection( ), fn:doc( ), and fn:doc-available( ).

Methodology

FIG. 3 illustrates an method for transforming between relational dataand XML data, in accordance with one embodiment of the presentinvention. This method can be carried out, for example, by the XQuerymodule 120 of FIGS. 1 and 2. Recall that the module 120 can beimplemented in software, hardware, firmware, or some combinationthereof. Further recall that the module 120 can be embedded, andinstalls like a driver.

The method begins with receiving 305 an XQuery query from an application(e.g., Java application). In one embodiment, this step is carried out bythe XQuery API for Java (XQJ) module 205, as previously explained.

The method proceeds with analyzing 310 the XQuery query, and dividing itinto one or multiple XQuery expressions to be processed. In the casewhere both XML and relational data sources are being queried at the sametime using a single XQuery query, some of the XQuery expressions can berelational queries and other XQuery expressions can be XML queries. Eachof these one or more XQuery expressions can then be processed (e.g.,individually and/or in parallel). In particular; for each of the one ormore XQuery expressions, the method continues with determining 315 if anXML or relational data source is being queried, or both XML andrelational data sources are being queried. Note that in one particularembodiment, the functionality of steps 310 and 315 are carried out bythe mediator module 210, as previously explained. The mediator module210 can then send the query to the appropriate XML or relationaladapters (as in steps 320 and 340).

If a relational data source is being queried, then the method continueswith sending 320 the XQuery query to one or more SQL adaptors, andtranslating 325 the XQuery query into SQL. The method continues withquerying 330 the relational database with the SQL query, and thenreceiving 335 the SQL query results and mapping them into XML. Note thatthe functionality of steps 325, 330, and 335 can be carried out, forexample, by the one or more of the SQL adapters 215, as previouslyexplained with reference to FIG. 2.

If, on the other hand, an XML data source is being queried, the methodcontinues sending 340 the XQuery query to one or more XML adaptors. Themethod then proceeds with querying 345 the XML source with the XQueryquery, and receiving 350 the XML XQuery query results. Here, thefunctionality of steps 345 and 350 can be carried out, for example, bythe one or more of the XML adapters 220, as previously explained.

The method continues with combining 355 results from multiple sources,if the case where both XML and relational sources are accessed, or morethan one of either relational or XML data sources. In one particularembodiment, the functionality of step 355 is carried out by the mediatormodule 210, as previously explained. The method then continues withsending 360 the results as XML to the requesting application using, forexample, the XQuery API for Java (e.g., XQJ module 205).

The foregoing description of the embodiments of the invention has beenpresented for the purposes of illustration and description. It is notintended to be exhaustive or to limit the invention to the precise formdisclosed. Many modifications and variations are possible in light ofthis disclosure. It is intended that the scope of the invention belimited not by this detailed description, but rather by the claimsappended hereto.

What is claimed is:
 1. A computer-implemented method comprising:receiving by a processor an XQuery query from a Java application via anXQuery API for Java (XQJ), the Java application attempting to access arelational data source having a relational data model; generating by theprocessor XML results responsive to the XQuery query, based on data fromthe relational data source by: dividing the XQuery query into one ormore XQuery expressions; determining for the one or more XQueryexpressions if an XML data source or the relational data source is beingqueried; retrieving, by an XQuery engine having a first data modelincompatible with the relational data model, the relational data if therelational data source is being queried; and executing in the XQueryengine at least one XQuery expression using the retrieved relationaldata if the relational data source is being queried; and sending the XMLresults to the Java application via the XQJ.
 2. The method of claim 1,wherein generating the XML results responsive to the XQuery queryfurther comprises combining results from multiple data sources.
 3. Themethod of claim 1, wherein in response to relational data sourcequeries, generating the XML results comprises: translating the XQueryquery into an SQL query; querying the relational data source with theSQL query; receiving one or more SQL query results; and mapping the oneor more SQL query results into XML.
 4. The method of claim 1, wherein inresponse to XML data source queries, generating the XML resultscomprises: querying the XML data source with the XQuery query; andreceiving one or more XML XQuery query results.
 5. The method of claim1, further comprising: combining the XML results generated from multiplesources.
 6. The method of claim 1, further comprising: performingcomparisons using value comparison operators.
 7. The method of claim 1,further comprising: caching a number of executed queries for reuse. 8.The method of claim 1, wherein generating the XML results responsive tothe XQuery query comprises retrieving both data stored in relationalform and data stored in XML form responsive to a single XQuery query. 9.A machine-readable storage medium having machine-executableinstructions, that when executed by a processor, cause the processor tocarry out a process comprising: receiving an XQuery query from a Javaapplication via an XQuery API for Java (XQJ), the Java applicationattempting to access a relational data source having a relational datamodel; generating XML results responsive to the XQuery query based ondata from the relational data source by: dividing the XQuery query intoone or more XQuery expressions; determining for the one or more XQueryexpressions if an XML data source or the relational data source is beingqueried; retrieving, by an XQuery engine having a first data modelincompatible with the relational data model, the relational data if therelational data source is being queried; and executing in the XQueryengine at least one XQuery expression using the retrieved relationaldata if the relational data source is being queried; and sending the XMLresults to the Java application via the XQJ.
 10. The machine-readablestorage medium of claim 9, wherein generating the XML results responsiveto the XQuery query further comprises combining results from multipledata sources.
 11. The machine-readable storage medium of claim 9,wherein in response to relational data source queries, generating theXML results comprises: translating the XQuery query into an SQL query;querying the relational data source with the SQL query; receiving one ormore SQL query results; and mapping the one or more SQL query resultsinto XML.
 12. The machine-readable storage medium of claim 9, wherein inresponse to XML data source queries, generating the XML resultscomprises: querying the XML data source with the XQuery query; andreceiving one or more XML XQuery query results.
 13. The machine-readablestorage medium of claim 9, wherein the process further comprises:combining the XML results generated from multiple sources.
 14. Themachine-readable storage medium of claim 9, wherein the process furthercomprises: performing comparisons using value comparison operators. 15.The machine-readable storage medium of claim 9, wherein the processfurther comprises: caching a number of executed queries for reuse. 16.The machine-readable storage medium of claim 9, wherein generating theXML results responsive to the XQuery query comprises retrieving bothdata stored in relational form and data stored in XML form responsive toa single XQuery query.
 17. A system comprising: a processor; and amachine-readable storage medium having machine-executable instructions,that when executed by the processor, cause the processor to carry out aprocess for transforming between the relational data and the XML data,the process implementing: an XQuery interface for receiving an XQueryquery from an application attempting to access a relational data sourcehaving a relational data model; and an XQuery engine, the XQuery enginehaving a first data model incompatible with the relational data model,for generating XML results responsive to the XQuery query based on datafrom the relational data source by: dividing the XQuery query into oneor more XQuery expressions; retrieving the relational data if therelational data source is being queried; executing at least one XQueryexpression using the retrieved relational data if the relational datasource is being queried; and sending the XML results to the applicationvia the XQuery interface; and wherein the XQuery engine includes amediator module for determining for the one or more XQuery expressionsif an XML data source or the relational data source is being queried.18. The system of claim 17, wherein the XQuery interface is an XQueryAPI for Java (XQJ) module and the application is a Java application. 19.The system of claim 17, wherein the mediator module is furtherconfigured for combining results from multiple data sources.
 20. Themethod of claim 1, further comprising: processing the XQuery query usingan XQPreparedExpression to create a prepared query, the XQuery queryprepared once and available for multiple executions; and caching theprepared query in a query pool for reuse.